/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.restaurant.facade;

import com.restaurant.entities.Orders;
import com.restaurant.entities.ProductTypes;
import com.restaurant.entities.Products;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Dexter
 */
@Stateless
public class ProductsFacade extends AbstractFacade<Products> implements ProductsFacadeLocal {
    @PersistenceContext(unitName = "com.restaurant_Restaurant_ZPHE-ejb_ejb_1.0-SNAPSHOTPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ProductsFacade() {
        super(Products.class);
    }
    
    @Override
    public void create(Products entity) {
        getEntityManager().persist(entity);
    }
    
    @Override
    public void edit(Products entity) {
        getEntityManager().merge(entity);
    }

    @Override
    public List<Products> getProducts(ProductTypes selected) {
        return em.createQuery("SELECT p FROM Products p WHERE p.productTypeId = :type" )
                .setParameter("type", selected).getResultList();
    }

    @Override
    public List getOrderProducts(Orders selected) {
        return em.createQuery("SELECT p FROM ProductsInOrders op INNER JOIN op.productId p WHERE op.orderId = :order" )
                .setParameter("order", selected).getResultList();
    }
}
